<script setup lang="ts">//lang="ts"严格语法 识别js
import { reactive, ref } from "vue";
import request from "@/utils/request.ts";
import {ElMessage, ElMessageBox, type UploadProps} from "element-plus";
import router from "@/router/index.ts";

const formRef = ref()
const data = reactive({
  form: {},
  rules: {
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
      { min: 1,max:4, message: '长度4位', trigger: 'blur' },
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6,max:6, message: '密码6位', trigger: 'blur' },
    ],
    phone: [
      { required: true, message: '请输入手机号', trigger: 'blur' },
      { min: 11,max:11, message: '手机号为11位数', trigger: 'blur' },
    ],
  }
})
const register = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      request.post('/insertUser', data.form).then(res => {
        if (res.status === '200') {
            ElMessage.success('注册成功')
            router.push('/login')
        } else {
          ElMessage.error(res.msg)
        }
      })
    }
  })
}
//更头像上传钩子函数
//图片发送成功后回调
const handleAvatarSuccess: UploadProps['onSuccess'] = (response, uploadFile) => {
  //imageUrl.value = URL.createObjectURL(uploadFile.raw!)
  console.log(response)
  data.form.avatar = response.data;//获取服务器返回的图片可访问路径
}
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (!(rawFile.type === 'image/jpeg' || rawFile.type === 'image/png')) {
    ElMessage.error('Avatar picture must be JPG Or PNG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  return true
}
</script>
<template>
  <div class="bg">
    <div style="width: 350px; background-color: #fff; border-radius: 5px; box-shadow: 0 0 10px rgba(0,0,0,0.1); padding: 40px 20px">
      <el-form status-icon ref="formRef" :model="data.form" :rules="data.rules">
        <div style="margin-bottom: 40px; text-align: center; font-weight: bold; font-size: 24px">欢 迎 注 册</div>
        <el-form-item label="头像">
          <el-upload
              class="avatar-uploader"
              action="http://localhost:8080/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
          >
            <img v-if="data.form.avatar" :src="`http://localhost:8080${data.form.avatar}`" class="avatar"  alt=""/>
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item prop="username">
          <el-input size="large" v-model="data.form.username" autocomplete="off" prefix-icon="User" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input size="large" show-password v-model="data.form.password" autocomplete="off" prefix-icon="Lock" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item prop="phone">
          <el-input size="large" v-model="data.form.phone" autocomplete="off" prefix-icon="Phone" placeholder="请输入手机号" />
        </el-form-item>
        <div style="margin-bottom: 20px">
          <el-button style="width: 100%; background-color: #248243; border-color: #248243" size="large" type="primary" @click="register">注 册</el-button>
        </div>
        <div style="text-align: right">
          已有账号？请 <a style="color: #248243" href="/login">登录</a>
        </div>
      </el-form>
    </div>
  </div>
</template>
<style scoped>
.bg {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  background: url("../assets/images/1.jpg");
  background-size: cover;
}
.avatar-uploader .avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>